package zjc.bookkeeping.tools;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import zjc.bookkeeping.R;

public class TimeDialog extends Dialog implements View.OnClickListener {
    TextView ymdTv,hmTv;
    LinearLayout ymdLl,hmLl,btnLl;
    TimePicker timePicker;
    DatePicker datePicker;
    Button suerBtn,cancelBtn;
    int year,month,dayOfMonth,hour,minute;

    public interface OnTimeSureListener{
        public void onTimeSure(String time,int year,int month,int day);
    }
    OnTimeSureListener onTimeSureListener;

    public void setOnTimeSureListener(OnTimeSureListener onTimeSureListener) {
        this.onTimeSureListener = onTimeSureListener;
    }

    public TimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_time);
        initView();
    }

    private void initView() {
        ymdTv = findViewById(R.id.dialog_time_ymd);
        hmTv = findViewById(R.id.dialog_time_hm);
        ymdLl = findViewById(R.id.dialog_time_ymd_ll);
        hmLl = findViewById(R.id.dialog_time_hm_ll);
        hmLl.setVisibility(View.GONE);
        btnLl = findViewById(R.id.dialog_time_btn_ll);
        timePicker = findViewById(R.id.dialog_time_tp);
        timePicker.setIs24HourView(true);
        timePicker.setVisibility(View.GONE);

        datePicker = findViewById(R.id.dialog_time_dp);
        suerBtn = findViewById(R.id.dialog_time_btn_sure);
        cancelBtn = findViewById(R.id.dialog_time_btn_cancel);

        ymdTv.setOnClickListener(this);
        hmTv.setOnClickListener(this);
        suerBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_time_btn_cancel:
                cancel();
                break;
            case R.id.dialog_time_btn_sure:
                year = datePicker.getYear();
                month = datePicker.getMonth() + 1;
                dayOfMonth = datePicker.getDayOfMonth();
                String monthStr = String.valueOf(month);
                if (month < 10){
                    monthStr = "0" + month;
                }
                String dayStr = String.valueOf(dayOfMonth);
                if (dayOfMonth < 10){
                    dayStr = "0" + dayOfMonth;
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    hour = timePicker.getHour();
                    minute = timePicker.getMinute();
                }
                String hourStr = String.valueOf(hour);
                String minuteStr = String.valueOf(minute);
                if (hour < 10){
                    hourStr = "0" + hour;
                }
                if (minute < 10){
                    minuteStr = "0" + minute;
                }
                String timeFormat = year+"年"+monthStr+"月"+dayStr+"日 "+hourStr+":"+minuteStr;
                if (onTimeSureListener != null){
                    onTimeSureListener.onTimeSure(timeFormat,year,month,dayOfMonth);
                }
                cancel();
                break;
            case R.id.dialog_time_ymd:
                showDatePicker();
                break;
            case R.id.dialog_time_hm:
                showTimePicker();
                break;
        }
    }

    private void showDatePicker() {
        datePicker.setVisibility(View.VISIBLE);
        timePicker.setVisibility(View.GONE);

        ymdLl.setVisibility(View.VISIBLE);
        hmLl.setVisibility(View.GONE);

        ymdTv.setBackgroundColor(Color.parseColor("#729FCF"));
        hmTv.setBackgroundColor(Color.parseColor("#C5E0FB"));
    }

    private void showTimePicker() {
        datePicker.setVisibility(View.GONE);
        timePicker.setVisibility(View.VISIBLE);

        ymdLl.setVisibility(View.GONE);
        hmLl.setVisibility(View.VISIBLE);

        ymdTv.setBackgroundColor(Color.parseColor("#C5E0FB"));
        hmTv.setBackgroundColor(Color.parseColor("#729FCF"));
    }
}